package game.memc.login;

import game.memc.utils.MemcacheInit;

import org.apache.log4j.Logger;

import MemCache.MemCacheMethod;

/**
 * Cache Dao used for login data.
 * 
 * @author Linzy
 * Create Date: 2009 12 4 14:15:39
 */
public class LoginCacheDB {
	private static final Logger logger = Logger.getLogger(LoginCacheDB.class);
	
	public static final String keyWapVer = "Login_keyWapVer_";
	public static final String keyPID = "Login_keyPID_";

    private static MemCacheMethod mem = MemcacheInit.initMemCache();

    /**
     * Get the request wap version.
     * @param uId
     * @return
     */
	public static String getWapVer(String uId) {
		return getStrCache(keyWapVer + uId);
	} // getWapVer()

	/**
	 * Set the request wap version.
	 * @param uId
	 * @param wapVer
	 * @return
	 */
	public static boolean setWapVer(String uId, String wapVer) {
		if(wapVer == null || wapVer.equals("")){
			return false;
		}
		return setCache(keyWapVer + uId, wapVer);
	}
	
	/**
	 * Set the cache object. 
	 * 
	 * @param key
	 * @param val
	 * @return
	 */
	private static boolean setCache(String key, Object val){
		try {
			mem.set(key, val);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	} // setCache()

	/**
	 * Get a cache String by key.
	 * 
	 * @param key
	 * @return
	 */
	private static String getStrCache(String key){
		try {
			return (String)mem.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	} // getStrCache()

	/**
	 * Clear the wapVer in cache.
	 * 
	 * @param uId
	 * @return
	 */
	public static boolean clearWapVer(String uId) {
		return clearCache(keyWapVer + uId);
	}

	/**
	 * Clear a cache.
	 * 
	 * @param k
	 * @return
	 */
	private static boolean clearCache(String k) {
		try {
			mem.delete(k);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Get the playerId from cache.
	 * @param userId String;
	 * @return
	 */
	public static String getPId(String userId) {
		return getStrCache(keyPID + userId);
	}
	
	/**
	 * Set the playerId to cache.
	 * 
	 * @param userId
	 * @param pId
	 * @return
	 */
	public static boolean setPId(String userId, String pId){
		String k = keyPID + userId;
		return setCache(k, pId);
	}
    
} // end of class
